<template>
  <div>
    <div class="bg-img"></div>
    <el-card class="card-login">
      <div class="mar-ver pad-btm text-align-center">
        <h3 class="h4 margin-no">后台管理系统</h3>
        <p class="text-gray">使用您的账号密码进行登陆</p>
      </div>
      <el-form ref="loginForm" :model="loginForm" :rules="loginRules" label-position="left" >
        <el-form-item prop="username">
          <el-input type="text" name="username" v-model="loginForm.username" autocomplete="on" placeholder="请输入用户名"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input :type="passwordType" name="password" v-model="loginForm.password" autocomplete="on" placeholder="请输入密码"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="button" class="el-button el-button--primary" :loading="loading" style="width: 100%;" @click="submitForm">立即登陆</el-button>
        </el-form-item>
        <el-form-item class="bord-top">
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
export default {
  name: 'login',
  data: function () {
    const validateUsername = (rule, value, callback) => {
      // 还需要通过正则去处理
      if (!value) {
        callback(new Error('请输入用户名'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      // 还需要通过正则去处理
      if (!value) {
        callback(new Error('请输入密码'))
      } else if (value.length < 6) {
        callback(new Error('密码长度不能小于6位'))
      } else {
        callback()
      }
    }
    return {
      passwordType: 'password',
      loading: false,
      loginForm: {
        username: 'admin',
        password: '123456'
      },
      loginRules: {
        username: [{required: true, trigger: 'blur', validator: validateUsername}],
        password: [{required: true, trigger: 'blur', validator: validatePassword}]
      }
    }
  },
  methods: {
    showPwd () {
      // 显示密码
      if (this.passwordType === 'password') {
        this.passwordType = 'text'
      } else {
        this.passwordType = 'password'
      }
    },
    submitForm () {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store.dispatch('Login', this.loginForm).then(() => {
            this.loading = false
            this.$router.push({path: '/'}) // 登陆成功之后重定向到首页
          }).catch(() => {
            this.loading = false
          })
        } else {
          console.log('参数验证不合法！')
          return false
        }
      })
    }
  }
}
</script>

<style scoped>
  .bg-img {
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    opacity: .8;
    background-position: center center;
    background-repeat: no-repeat;
    background-attachment: fixed;
    -webkit-background-size: cover;
    -moz-background-size: cover;
    -o-background-size: cover;
    background-image: url("../../assets/img/bg-img/bg-img-3.jpg");
    background-size: cover;
  }
  .card-login{
    position: absolute;
    left: 0;
    right: 0;
    width: 360px;
    margin: 140px auto;
  }
  .mar-ver {
    margin-top: 15px;
    margin-bottom: 15px;
  }
  .pad-btm {
    padding-bottom: 15px;
  }
  .text-align-center{
    text-align: center;
  }
  .h4 {
    font-weight: 600;
    color: #2b425b;
  }
  .margin-no {
    margin: 0 !important;
  }
  .text-gray {
    color: #afb9c3;
  }
</style>
